﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI.Commands;
using WPFCABShell.Shell.Interface;
using WPFCABShell.Shell.Interface.Services.Command;
using WPFCABShell.Shell.Interface.Services.ToolBar;
using Microsoft.Practices.ObjectBuilder;
using CAB = Microsoft.Practices.CompositeUI.Commands;
using WPF = System.Windows.Input;

namespace ToolBarDemo
{
	public sealed class ToolBarDemoPresenter : Presenter<IToolBarDemoView>
	{
		private readonly ICommandService _commandService;
		private readonly IToolBarService _toolBarService;
		private const string _toolBarSiteName = "site://ToolBar/Tool Bar Demo";

		public ToolBarDemoPresenter([Dependency] ICommandService commandService, [Dependency] IToolBarService toolBarService)
		{
			_commandService = commandService;
			_toolBarService = toolBarService;
		}

		#region Example 1

		private IToolBarItem _item;

		public bool Example1CanAddToolBarItem()
		{
			return _item == null;
		}

		public void Example1AddToolBarItem()
		{
			ICommand command = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item", null);
			_item = _toolBarService.AddButtonItem(ExtensionSites.ToolBar.Edit.Name, command, null, null);
		}

		public bool Example1CanCleanUp()
		{
			return _item != null;
		}

		public void Example1CleanUp()
		{
			_toolBarService.Remove(_item);
			_item = null;
		}

		#endregion

		#region Example 2

		private IToolBarItem _itemInNewToolBar;

		public bool Example2CanAddToolBarItem()
		{
			return _itemInNewToolBar == null;
		}

		public void Example2AddToolBarItem()
		{
			ICommand command = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item", null);
			_itemInNewToolBar = _toolBarService.AddButtonItem("Some Tool Bar", command, null, null);
		}

		public bool Example2CanCleanUp()
		{
			return _itemInNewToolBar != null;
		}

		public void Example2CleanUp()
		{
			_toolBarService.Remove(_itemInNewToolBar);
			_itemInNewToolBar = null;
		}

		#endregion

		#region Example 3

		private IToolBarItem _item1;
		private IToolBarItem _item2;
		private IToolBarItem _item3;
		private IToolBarItem _item4;

		public bool Example3CanAddToolBarItems()
		{
			return _item1 == null;
		}

		public void Example3AddToolBarItems()
		{
			ICommand command1 = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item 1", null);
			ICommand command2 = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item 2", null);
			ICommand command3 = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item 3", null);
			ICommand command4 = _commandService.CreateCommand(WorkItem.Commands["ToolBarItem"], "Tool Bar Item 4", null);
			_item1 = _toolBarService.AddButtonItem("Sorted and Grouped Bar", command1, 0, 0);
			_item3 = _toolBarService.AddButtonItem("Sorted and Grouped Bar", command3, 0, 2);
			_item2 = _toolBarService.AddButtonItem("Sorted and Grouped Bar", command2, 0, 1);
			_item4 = _toolBarService.AddButtonItem("Sorted and Grouped Bar", command4, 1, 0);
		}

		public bool Example3CanCleanUp()
		{
			return _item1 != null;
		}

		public void Example3CleanUp()
		{
			_toolBarService.Remove(_item1);
			_toolBarService.Remove(_item2);
			_toolBarService.Remove(_item3);
			_toolBarService.Remove(_item4);
			_item1 = null;
			_item2 = null;
			_item3 = null;
			_item4 = null;
		}

		#endregion

		[CommandHandler("ToolBarItem")]
		public void ToolBarItem(object sender, EventArgs e)
		{
			View.ShowToolBarItemClicked();
		}
	}
}
